package com.example.test.comutil;

import android.text.TextUtils;

import java.math.BigInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FormatUtil {

    /**
     * 去除String 前后的双引号
     *
     * @return
     */

    public static String removeQuotes(String jsonString) {
        if (jsonString == null) {
            return null;
        }
        if (jsonString.endsWith("\"")) {
            jsonString = jsonString.substring(0, jsonString.length());
        }
        if (jsonString.startsWith("\"") || jsonString.endsWith("\"")) {
            // 防止服务器传回来的字符串有多个双引号开头或结尾，递归一下。
            return removeQuotes(jsonString);
        }
        return jsonString;
    }

    public static boolean isPhone(String phone) {
        if (TextUtils.isEmpty(phone))
            return false;
        String str = "^1\\d{10}$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(phone);
        return m.matches();
    }

    public static boolean isNum(String phone) {
        if (null == phone)
            return false;
        String str = "^(?!0)(?:[0-9]{1,3}|1000)$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(phone);
        return m.matches();
    }
    public static boolean isFloatNum(String num){
        if (null == num || "".equals(num)) {
            return false;
        }
        Pattern pattern = Pattern.compile("^[-\\+]?[.\\d]*$");
        return pattern.matcher(num).matches();
    }

    /**
     * 将byte[]转为各种进制的字符串
     * @param bytes byte[]
     * @param radix 基数可以转换进制的范围，从Character.MIN_RADIX到Character.MAX_RADIX，超出范围后变为10进制
     * @return 转换后的字符串
     */
    public static String binary(byte[] bytes, int radix){
        return new BigInteger(1, bytes).toString(radix);// 这里的1代表正数
    }

    public static boolean isNumeric(String num)
    {
        Pattern pattern=Pattern.compile("[0-9]*");
        Matcher isnum=pattern.matcher(num);
        return isnum.matches();
    }

    /**
     *
     * @param message   验证内容
     * @param regular   正则表达式
     * @return
     */
    public static boolean isRegular(String message,String regular) {
        Pattern pattern=Pattern.compile(regular);
        Matcher isnum=pattern.matcher(message);
        return isnum.matches();
    }

    public static boolean isEmail(String email) {
        if (null == email)
            return false;
        String str = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(email);
        return m.matches();
    }

    public static boolean isPwdLengthValid(String pwd) {
        return null != pwd && pwd.length() >= 6 && pwd.length() <= 20;
    }

    public static boolean isPwdValid(String pwd) {
        if (pwd.length() < 6 || pwd.length() > 20) {
            return false;
        } else {
            String str=".*[a-zA-Z].*[0-9]|.*[0-9].*[a-zA-Z]";
            Pattern p = Pattern.compile(str);
            Matcher matcher = p.matcher(pwd);
            return matcher.matches();
        }
    }
}
